#!/bin/bash
# Begin post_install_reboot

echo "This will safely shut down the system and reboot the"
echo "computer when nothing else will. Run this in a tty"
echo "console, not in an X Windows terminal."
echo
echo -n "Ready to proceed? (y/n) "
read REPLY
if [ "$REPLY" != "y" ] ; then
    read -p "Press a key to abort."
    exit 0
fi

# Check for root privileges...
if [ $EUID -ne 0 ]; then
   echo
   echo "Must be run as root."
   echo
   exit 1
fi

echo "Stop system and kernel logging daemons"
kill -9 $(pidof klogd)
kill -9 $(pidof syslogd)

echo "Stop the udev daemon"
udevadm control --exit

echo "Send all processes the TERM signal"
pkill --inverse -s0,1 -TERM
sleep 5

echo "Send all processes the KILL signal"
pkill --inverse -s0,1 -KILL
sleep 5

echo "De-activate swap partitions"
swapoff -a

echo "Bring down loopback interface"
ip link set lo down

echo "Flush filesystem buffers"
echo s > /proc/sysrq-trigger
sleep 3

echo "Remount filesystems ro"
echo u > /proc/sysrq-trigger
sleep 3

# SysRq reboot...
echo b > /proc/sysrq-trigger

# End post_install_reboot
